/**
 * 本地 表数据
 */
class LocalSheetVo{
    private _byteArray:ByteArray;//二进制数据
    private _excelPath:string;//excel包路径
    private _excelName:string;//excel文件名
    private _sheetName:string;//表名
    private _className:string;//类名
    private _mainKey:string;//主键
    private _viceKey:string;//副键
    private _dataColNum:number;//数据列数
    private _dataRowNum:number;//数据行数

    private _fullPath:string;//完整路径

    private _voList:Array<any>;//数据列表
    private _voDic:Object;//数据map列表
    private _startPos:number;//从pos开始读取

    constructor(byteArray:ByteArray,startPos:number){
        this._voDic = {};
        this._startPos = startPos;
        this._byteArray = byteArray;
        //解析 二进制数据
        this.parser();
    }

    //解析 二进制数据
    private parser():void{
        this._byteArray.pos = this._startPos;
        this._excelPath =  this._byteArray.readString();
        this._excelName =  this._byteArray.readString();
        this._sheetName =  this._byteArray.readString();
        this._className =  this._byteArray.readString();
        this._mainKey =   this._byteArray.readString();
        this._viceKey =  this._byteArray.readString();
        this._mainKey = this._mainKey != "" ? this._mainKey.split("#")[1] : "";
        this._viceKey = this._viceKey != "" ? this._viceKey.split("#")[1] : "";
        this._byteArray.readInt();//this._arrayMode =
        this._dataColNum =  this._byteArray.readInt();
        this._dataRowNum =  this._byteArray.readInt();
        this._fullPath =  "data."  +  this._className;
        this._startPos = this._byteArray.pos;
    }

    //初始化 表数据
    private initSheetList():void{
        this._byteArray.pos = this._startPos;
        this. _voList = [];
        var valueType:String;
        var classVo:Object;
        var voClass:any = LocalData.getDefinitionByName(this._fullPath);
        var funList:Array<any> = [];
        var valueNameList:Array<any> = [];
        //读取 属性名 描述 类型
        for(var i:number=0;i<this._dataColNum;i++){
            valueNameList.push(this._byteArray.readString());
            this._byteArray.readString();
            valueType = this._byteArray.readString();
            //检测 属性类型
            if(valueType == "number"){
                funList.push("readInt");
            }else if(valueType == "string"){
                funList.push("readUTF");
            }else{
                funList.push("readArray");
            }
        }
        //获取 键值
        var mainKey:string = this._mainKey != "" ? valueNameList[this.getColIndex(this._mainKey)] : "";
        var viceKey:string = this._viceKey != "" ? valueNameList[this.getColIndex(this._viceKey)] : "";
        //读取 属性值
        var valueName:string;
        for(var j:number=0;j<this._dataRowNum;j++){
            classVo = new voClass();
            for(var k:number=0;k<funList.length;k++){
                valueName = valueNameList[k];
                classVo[valueName] = this[funList[k]]();
            }
            this._voList.push(classVo);
            if(viceKey != ""){
                this._voDic[classVo[mainKey]+"_"+classVo[viceKey]] = classVo;
            }else{
                this._voDic[classVo[mainKey]] = classVo;
            }
        }
    }

    /**
     * 读取一个int
     * @return
     */
    private readInt():number{
        return this._byteArray.readInt();
    }

    /**
     * 读取一个字符串
     * @return
     */
    private readUTF():string{
        return this._byteArray.readString();
    }

    /**
     * 读取一个字符串 转换为数组
     * @return
     */
    private readArray():Array<any>{
        var str:string = this._byteArray.readString();
        if(str == ""){
            return [];
        }
        var json:string = "["+ str +"]";
        return JSON.parse(json);
    }

    /**
     * 获取 vo 数据
     * @param key
     * @return
     */
    public getClassVo(key:string):Object{
        var vo:Object = this._voDic[key];
        if(vo != null){
            return vo;
        }
        //如果 表数据为空 初始化表数据
        if(this._voList == null){
            this.initSheetList();
        }
        return this._voDic[key];
    }

    /**
     * 获取 vo 列表
     * @return
     */
    public get voList():Array<any>{
        //如果 表数据为空 初始化表数据
        if(this._voList == null){
            this.initSheetList();
        }
        return this._voList;
    }

    /**
     * 获取 vo 列表
     * @return
     */
    public get voDic():Object{
        //如果 表数据为空 初始化表数据
        if(this._voList == null){
            this.initSheetList();
        }
        return this._voDic;
    }

    /**
     * 获取 表 完整路径
     * @return
     */
    public get fullPath():string{
        return this._fullPath;
    }

    /**
     * 根据列名(A B) 获取 列的index
     * @param colName
     * @return
     */
    private getColIndex(colName:string):number{
        var abc:String = colName.replace(/(\d)/g,"");
        var returnValue:number = 0;
        for(var i:number=abc.length-1;i>=0;i--){
            var cValue:number = abc.charCodeAt(i) - 65;
            returnValue = returnValue + cValue + (i * 26);
        }
        return returnValue;
    }
}